package com.cardbaobao.cardbabyclient.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;

/**
 * 横向滚动标题栏
 * Created by zad
 * 2016/8/10 11:15
 */

public class HorizontalScrollTitleView extends HorizontalScrollView implements RadioGroup.OnCheckedChangeListener {

    private Context mContext;
    /**
     * 字体大小
     */
    private int mTextSize = 14;
    /**
     * RadioGroup 背景色
     */
    private int mRootBackground = Color.parseColor("#E4E4E4");
    /**
     * RadioButton 字体选中颜色
     */
    private int mItemCheckedTextColor = Color.parseColor("#FFFFFF");
    /**
     * RadioButton 字体未选中颜色源ID
     */
    private int mItemUnCheckTextColor = -1;
    /**
     * RadioButton 字体未选中颜色集合
     */
    private ColorStateList mItemUnCheckTextColorStateList;

    /**
     * RadioButton 背景选中源ID
     */
    private int mItemCheckedBackground;
    /**
     * RadioButton 背景未选中源ID
     */
    private int mItemUnCheckBackground;
    /**
     * RadioGroup 内边距
     */
    private int mRootPadding = 0;
    /**
     * RadioButton 内边距
     */
    private int mItemPadding = 0;
    /**
     * RadioButton 左内边距
     */
    private int mItemPaddingLeft = 0;
    /**
     * RadioButton 右内边距
     */
    private int mItemPaddingRight = 0;
    /**
     * RadioButton 顶内边距
     */
    private int mItemPaddingTop = 0;
    /**
     * RadioButton 底部内边距
     */
    private int mItemPaddingBottom = 0;
    /**
     * RadioButton 外边距
     */
    private int mItemMargin = 0;

    private int mItemMarginLeft = 0;

    private int mItemMarginRight = 0;

    private int mItemMarginTop = 0;

    private int mItemMarginBottom = 0;


    private RadioGroup mRadioGroup;

    private int mScreenWidth;
    /**
     * 上一个被单击的Item位置
     */
    private int mLastPos;

    private OnItemCheckedListener onItemCheckedListener;

    public HorizontalScrollTitleView(Context context) {
        this(context, null);
    }

    public HorizontalScrollTitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalScrollTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mContext = context;

        TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.HorizontalScrollTitleView);
        mRootBackground = array.getColor(R.styleable.HorizontalScrollTitleView_rootBackground, mRootBackground);
        mItemCheckedTextColor = array.getColor(R.styleable.HorizontalScrollTitleView_itemCheckedTextColor, mItemCheckedTextColor);
        mItemUnCheckTextColor = array.getResourceId(R.styleable.HorizontalScrollTitleView_itemUnCheckTextColor, mItemUnCheckTextColor);
        mItemCheckedBackground = array.getResourceId(R.styleable.HorizontalScrollTitleView_itemCheckedBackground, mItemCheckedBackground);
        mItemUnCheckBackground = array.getResourceId(R.styleable.HorizontalScrollTitleView_itemUnCheckBackground, mItemUnCheckBackground);

        mRootPadding = (int) array.getDimension(R.styleable.HorizontalScrollTitleView_rootPadding, mRootPadding);

        mItemPadding = (int) array.getDimension(R.styleable.HorizontalScrollTitleView_itemPadding, mItemPadding);
        mItemPaddingLeft = (int) array.getDimension(R.styleable.HorizontalScrollTitleView_itemPaddingLeft, mItemPaddingLeft);
        mItemPaddingRight = (int) array.getDimension(R.styleable.HorizontalScrollTitleView_itemPaddingRight, mItemPaddingRight);
        mItemPaddingTop = (int) array.getDimension(R.styleable.HorizontalScrollTitleView_itemPaddingTop, mItemPaddingTop);
        mItemPaddingBottom = (int) array.getDimension(R.styleable.HorizontalScrollTitleView_itemPaddingBottom, mItemPaddingBottom);

        mItemMargin = (int) array.getDimension(R.styleable.HorizontalScrollTitleView_itemMargin, mItemMargin);
        mItemMarginLeft = (int) array.getDimension(R.styleable.HorizontalScrollTitleView_itemMarginLeft, mItemMarginLeft);
        mItemMarginRight = (int) array.getDimension(R.styleable.HorizontalScrollTitleView_itemMarginRight, mItemMarginRight);
        mItemMarginTop = (int) array.getDimension(R.styleable.HorizontalScrollTitleView_itemMarginTop, mItemMarginTop);
        mItemMarginBottom = (int) array.getDimension(R.styleable.HorizontalScrollTitleView_itemMarginBottom, mItemMarginBottom);

        mTextSize = array.getInteger(R.styleable.HorizontalScrollTitleView_itemTextSize, mTextSize);

        array.recycle();

        mRadioGroup = new RadioGroup(mContext);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        mRadioGroup.setBackgroundColor(mRootBackground);
        mRadioGroup.setGravity(Gravity.CENTER_VERTICAL);
        mRadioGroup.setOrientation(LinearLayout.HORIZONTAL);
        mRadioGroup.setPadding(mRootPadding, mRootPadding, mRootPadding, mRootPadding);
        mRadioGroup.setOnCheckedChangeListener(this);
        addView(mRadioGroup, params);//添加RadioGroup

        mScreenWidth = ScreenUtil.getScreenWidth(mContext);

    }

    /**
     * 设置所有Item不选中状态
     */
    public void setAllItemUnChecked() {
        try {
            if (mRadioGroup != null) {
                RadioButton mRadioBtn = (RadioButton) mRadioGroup.getChildAt(mLastPos);
                mRadioBtn.setBackgroundResource(mItemUnCheckBackground);
                mRadioBtn.setTag(mItemUnCheckBackground);
                if (mItemUnCheckTextColorStateList != null) {
                    mRadioBtn.setTextColor(mItemUnCheckTextColorStateList);
                } else {
                    mRadioBtn.setTextColor(mItemUnCheckTextColor);
                }
                mLastPos = -1;
            }
        } catch (Exception e) {

        }
    }

    /**
     * 添加数据源
     *
     * @param mData
     */
    public void setDataSource(String[] mData) {
        if (mData != null && mData.length > 0) {
            mRadioGroup.removeAllViews();
            for (int i = 0; i < mData.length; i++) {
                final RadioButton mRadioBtn = new RadioButton(mContext);
                mRadioBtn.setTextSize(mTextSize);
                RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RadioGroup.LayoutParams.WRAP_CONTENT);
                if (mItemPadding != 0) {
                    mRadioBtn.setPadding(mItemPadding, mItemPadding, mItemPadding, mItemPadding);
                } else {
                    mRadioBtn.setPadding(mItemPaddingLeft, mItemPaddingTop, mItemPaddingRight, mItemPaddingBottom);
                }
                mRadioBtn.setGravity(Gravity.CENTER);
                mRadioBtn.setText(mData[i]);
                mRadioBtn.setId(i);
                mItemUnCheckTextColorStateList = ContextCompat.getColorStateList(mContext, mItemUnCheckTextColor);
                if (mItemUnCheckTextColorStateList != null) mRadioBtn.setTextColor(mItemUnCheckTextColorStateList);
                mRadioBtn.setBackgroundResource(mItemUnCheckBackground);
                mRadioBtn.setTag(mItemUnCheckBackground);
                mRadioBtn.setButtonDrawable(android.R.color.transparent);

                if (i == 0) {
                    //默认选中第0个Item
                    mLastPos = 0;
                    mRadioBtn.setBackgroundResource(mItemCheckedBackground);
                    mRadioBtn.setTag(mItemCheckedBackground);
                    mRadioBtn.setTextColor(mItemCheckedTextColor);
                }
                if (mItemMargin != 0) {
                    params.setMargins(mItemMargin, mItemMargin, mItemMargin, mItemMargin);
                } else {
                    params.setMargins(mItemMarginLeft, mItemMarginTop, mItemMarginRight, mItemMarginBottom);
                }
                mRadioBtn.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isSelected = false;//当前是否选中
                        //item单击事件
                        if(mLastPos == mRadioBtn.getId()){
                            //上次单击和本次单击是一个位置
                            if(mRadioBtn.getTag() != null){
                                int bgColor = (int) mRadioBtn.getTag();
                                if(bgColor == mItemCheckedBackground){
                                    //选中，则设置未选中
                                    mRadioBtn.setBackgroundResource(mItemUnCheckBackground);
                                    mRadioBtn.setTag(mItemUnCheckBackground);
                                    mRadioBtn.setTextColor(mItemUnCheckTextColor);
                                    if (mItemUnCheckTextColorStateList != null) mRadioBtn.setTextColor(mItemUnCheckTextColorStateList);
                                    isSelected = false;
                                }else{
                                    //未选中，则设置选中
                                    mRadioBtn.setBackgroundResource(mItemCheckedBackground);
                                    mRadioBtn.setTag(mItemCheckedBackground);
                                    mRadioBtn.setTextColor(mItemCheckedTextColor);
                                    isSelected = true;
                                }
                            }
                        }else{
                            //上次单击和本次单击不是一个位置
                            mRadioBtn.setBackgroundResource(mItemCheckedBackground);
                            mRadioBtn.setTag(mItemCheckedBackground);
                            mRadioBtn.setTextColor(mItemCheckedTextColor);
                            if (mLastPos != -1) {
                                RadioButton rBtn = (RadioButton) mRadioGroup.getChildAt(mLastPos);
                                rBtn.setBackgroundResource(mItemUnCheckBackground);
                                rBtn.setTag(mItemUnCheckBackground);
                                if (mItemUnCheckTextColorStateList != null) rBtn.setTextColor(mItemUnCheckTextColorStateList);
                            }
                            isSelected = true;
                        }
                        mLastPos = mRadioBtn.getId();
                        if (onItemCheckedListener != null) {
                            onItemCheckedListener.onItemClick(mRadioBtn, mLastPos,isSelected);
                        }
                    }
                });
                mRadioGroup.addView(mRadioBtn, params);
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        View itemView = group.getChildAt(checkedId);
        //滚动保持选中的Item在中部位置显示
        smoothScrollTo(itemView.getLeft() - mScreenWidth / 2 + itemView.getWidth() / 2, 0);
    }

    public interface OnItemCheckedListener {
        /**
         *
         * @param item 被选中的View
         * @param position 当前单击位置
         * @param isSelected 是否被选中
         */
        void onItemClick(View item, int position,boolean isSelected);
    }

    public void setOnItemCheckedListener(OnItemCheckedListener onItemCheckedListener) {
        this.onItemCheckedListener = onItemCheckedListener;
    }
}
